import { Component, OnInit } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { SFSchema, SFUISchema } from '@delon/form';
import { Service } from '@api';

@Component({
  template: '',
})
export abstract class EditComponent<T, ID> implements OnInit {
  record: any = {};
  i: any;
  schema: SFSchema = {};
  ui: SFUISchema = {};

  constructor(public modal: NzModalRef, public msgSrv: NzMessageService, public http: _HttpClient, public service: Service<T, ID>) {}

  ngOnInit(): void {
    if (this.record.id > 0) this.service.findById(this.record.id).subscribe((res) => (this.i = res));
  }

  save(value: any) {
    this.service.save(value).subscribe((res) => {
      this.msgSrv.success('保存成功');
      this.modal.close(true);
    });
  }

  saveAs(value: any) {
    value.id = null;
    this.save(value);
  }

  close() {
    this.modal.destroy();
  }
}
